package algorithmExample;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author: zhang.rongqing
 * @since: 2019/10/24
 */
//给定一个字符串，请你找出其中不含有重复字符的 最长子串 的长度。
//
//        示例 1:
//
//        输入: "abcabcbb"
//        输出: 3
//        解释: 因为无重复字符的最长子串是 "abc"，所以其长度为 3。
//        示例 2:
//
//        输入: "bbbbb"
//        输出: 1
//        解释: 因为无重复字符的最长子串是 "b"，所以其长度为 1。
//        示例 3:
//
//        输入: "pwwkew"
//        输出: 3
//        解释: 因为无重复字符的最长子串是 "wke"，所以其长度为 3。
//             请注意，你的答案必须是 子串 的长度，"pwke" 是一个子序列，不是子串。
public class LongestSubStringWithoutRepeatingCharacter {
//    public int lengthOfLongestSubstring(String s) {
//        char[] charList = s.toCharArray();
//        Set result = new HashSet();
//        int maxLength = 0;
//        for (char a : charList) {
//            if (result.contains(a)) {
//                if (result.size() > maxLength) {
//                    maxLength = result.size();
//                }
//                result.clear();
//            }
//            result.add(a);
//        }
//        return maxLength > result.size() ? maxLength : result.size();
//    }

    /**
     * 两次循环干
     *
     * @param s
     * @return int
     */
    public int lengthOfLongestSubstring(String s) {
        char[] charArray = s.toCharArray();
        int maxLength = 0;
        Set result = new HashSet();
        for (int i = 0; i < charArray.length; i++) {
            for (int j = i; j < charArray.length; j++) {
                if (result.contains(charArray[j])) {
                    if (maxLength < result.size()) {
                        maxLength = result.size();
                    }
                    result.clear();
                    break;
                }
                result.add(charArray[j]);
            }
            maxLength = maxLength > result.size() ? maxLength : result.size();
            result.clear();
        }

        return maxLength;
    }

    /**
     * 滑动窗口，每次需要判断
     *
     * @param s
     * @return int
     */
    public int lengthOfLongestSubstring2(String s) {
        int start = 0, end = 0, maxLength = 0;
        char[] charArray = s.toCharArray();
        int length = charArray.length;
        Set result = new HashSet();
        while (start < length & end < length) {
            if (result.contains(charArray[end])) {
                maxLength = result.size() > maxLength ? result.size() : maxLength;
                result.remove(charArray[start++]);
//                start++;
            } else {
                result.add(charArray[end++]);
            }
        }
        return result.size() > maxLength ? result.size() : maxLength;
    }

    /**
     * HashMap<值，指针的位置>
     *
     * @param s
     * @return int
     */
    public int lengthOfLongestSubstring3(String s) {
        int length = s.length();
        int maxLength = 0;
        Map<Character, Integer> result = new HashMap<Character, Integer>();
        for (int start = 0, end = 0; end < length; end++) {
            if (result.containsKey(s.charAt(end))) {
                //防止出现类似于"abba"的情况，扫描到"a"的时候识别出不在改范围
                start = Math.max(result.get(s.charAt(end)) + 1, start);
            }
            maxLength = maxLength > end - start + 1 ? maxLength : end - start + 1;
            result.put(s.charAt(end), end);
        }
        return maxLength;
    }

    public static void main(String[] args) {
        LongestSubStringWithoutRepeatingCharacter character = new LongestSubStringWithoutRepeatingCharacter();
        String str = "abcabcbb";
        System.out.println(String.format("character:%s, longest length is %s ", str, character.lengthOfLongestSubstring(str)));

        String str2 = "pwwkew";

        String str3 = "abba";
        System.out.println(String.format("character:%s, longest length is %s ", str2, character.lengthOfLongestSubstring(str2)));

        System.out.println(String.format("character:%s, longest length is %s ", str, character.lengthOfLongestSubstring2(str)));
        System.out.println(String.format("character:%s, longest length is %s ", str2, character.lengthOfLongestSubstring2(str2)));

        System.out.println(String.format("character:%s, longest length is %s ", str, character.lengthOfLongestSubstring3(str)));
        System.out.println(String.format("character:%s, longest length is %s ", str2, character.lengthOfLongestSubstring3(str2)));
        System.out.println(String.format("character:%s, longest length is %s ", str3, character.lengthOfLongestSubstring3(str3)));
    }
}
